

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;light&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/me/title.jpg">
  <link rel="icon" type="image/png" href="/img/me/title.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="你要悄悄学web，然后惊艳所有人">
  <meta name="author" content="yxfan">
  <meta name="keywords" content="">
  <title>初识commander.js - yxfan</title>

  <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/highlight.js@10.4.0/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3","placement":"right","visible":"always","Option":"§ |","icon":"#"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":20},"lazyload":{"enable":true,"onlypost":false},"web_analytics":{"enable":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"AGrJP3tUCktoWpoiRigqj3zy-gzGzoHsz","app_key":"r0u61FlVNs3yvXwGYocWG3qm","server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>yxfan</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/background/lu_bule.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="初识commander.js">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2019-12-17 11:45" pubdate>
        2019年12月17日 中午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      824 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      10
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">初识commander.js</h1>
            
            <div class="markdown-body">
              <p>commander.js 是 express 框架的开发者号称无所不能的 TJ 大神写的工具包，用于快捷开发命令行工具，提高项目的开发效率。<a target="_blank" rel="noopener" href="https://github.com/tj/commander.js/blob/master/Readme_zh-CN.md">commander.js 官网地址</a></p>
<h3 id="初识命令行工具"><a href="#初识命令行工具" class="headerlink" title="初识命令行工具"></a>初识命令行工具</h3><p>下面是npm安装全局包的实例</p>
<div class="code-wrapper"><pre><code class="hljs javascript">npm install commander -g</code></pre></div>
<p>上述命令行代码由三部分组成：</p>
<ul>
<li>npm 命令行工具名称</li>
<li>install 指令，后面的 commander 为该指令携带的参数</li>
<li>-g 选项</li>
</ul>
<h3 id="定义命令行工具能够提供哪些选项"><a href="#定义命令行工具能够提供哪些选项" class="headerlink" title="定义命令行工具能够提供哪些选项"></a>定义命令行工具能够提供哪些选项</h3><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> program = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;commander&#x27;</span>);
program.option(flag, desc, fn, defaultValue)</code></pre></div>
<p>通过 option 方法我们可以定义命令行工具提供哪些选项，这些选项可以带参数，我们可以对参数进行自定义的再处理，我们还可以给参数赋值默认值。</p>
<ul>
<li>flag 必填。定义命令行参数能够提供哪些参数；</li>
<li>desc 非必填。对参数进行描述；</li>
<li>fn 非必填。是一个函数；</li>
<li>defaultValue 非必填。默认值;</li>
<li>&lt;&gt; 符号表示参数必填，不填会报错;</li>
<li>[] 符号表示参数非必填。</li>
</ul>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-comment">// 表示命令行提供 -i 参数，--install 参数是 -i 的别名，二者作用相同</span>
<span class="hljs-comment">// 如果指令用到了这个参数，那么 program.install 的值就变成 true，否则为 false</span>
program.option(<span class="hljs-string">&#x27;-i, --install&#x27;</span>)</code></pre></div>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-comment">// 第二个参数是对选项 -i 的描述信息</span>
program.option(<span class="hljs-string">&#x27;-i, --install &lt;packagename&gt;&#x27;</span>, <span class="hljs-string">&#x27;install some package&#x27;</span>)</code></pre></div>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-comment">// 更改 program.install 的值，变成 handle 函数的返回值</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handle</span> (<span class="hljs-params">arg</span>) </span>&#123;     <span class="hljs-comment">// 这里的 arg 等于选项后面跟着的参数 packagename 的值</span>
    <span class="hljs-comment">// 对 arg 处理然后返回处理后的值 afterarg，如将参数转换为数组并返回</span>
    <span class="hljs-keyword">return</span> arg.split(<span class="hljs-string">&#x27;,&#x27;</span>);
&#125;
<span class="hljs-comment">// 第三个参数为函数，名称可以任意取值</span>
program.option(<span class="hljs-string">&#x27;-i, --install &lt;packagename&gt;&#x27;</span>, <span class="hljs-string">&#x27;install some package&#x27;</span>, handle)</code></pre></div>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-comment">// 如果第三个参数不是函数而是字符串，表示是 packagename 的默认值，packagename 如果不输入值，就赋值默认值</span>
program.option(<span class="hljs-string">&#x27;-i, --install &lt;packagename&gt;&#x27;</span>, <span class="hljs-string">&#x27;install some package&#x27;</span>, <span class="hljs-string">&#x27;commander&#x27;</span>)</code></pre></div>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handle</span> (<span class="hljs-params">arg</span>) </span>&#123;
    <span class="hljs-comment">// 处理后返回值</span>
&#125;
<span class="hljs-comment">// 第三个参数是函数，第四个参数依然是 packagename 的默认值，如果 packagename 没有值，</span>
<span class="hljs-comment">// 就会将第一个参数的值赋值给 packagename，再将 packagename 作为参数传给 handle 函数</span>
program.option(<span class="hljs-string">&#x27;-i, --install &lt;packagename&gt;&#x27;</span>, <span class="hljs-string">&#x27;install some package&#x27;</span>, handle, <span class="hljs-string">&#x27;commander&#x27;</span>)</code></pre></div>
<h3 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h3><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> program = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;commander&#x27;</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">delfun</span> (<span class="hljs-params">arg</span>) </span>&#123;
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;接收到的参数 packagename 为&#x27;</span> + arg);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;可以对参数 arg 进行处理并返回&#x27;</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;返回值同时会赋值给 program.delete 属性&#x27;</span>);
&#125;

program
  .version(<span class="hljs-string">&#x27;0.0.1&#x27;</span>)
  .option(<span class="hljs-string">&#x27;-i, --install [packagename]&#x27;</span>, <span class="hljs-string">&#x27;install some package&#x27;</span>)
  .option(<span class="hljs-string">&#x27;-u, --update [packagename]&#x27;</span>, <span class="hljs-string">&#x27;update some package&#x27;</span>)
  .option(<span class="hljs-string">&#x27;-d, --del [packagename]&#x27;</span>, <span class="hljs-string">&#x27;delete some package&#x27;</span>, delfun, <span class="hljs-string">&#x27;defval&#x27;</span>)
  .parse(process.argv);
	
<span class="hljs-comment">// 命令行中使用到的参数会变成 true 进行标识，没有使用到的参数标识值为 false</span>
<span class="hljs-keyword">if</span> (program.install) <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;调用了 -i 参数，我们可以对 -i 参数做具体处理&#x27;</span>)
<span class="hljs-keyword">if</span> (program.update) <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;调用了 -u 参数，我们可以对 -u 参数做具体处理&#x27;</span>)
<span class="hljs-keyword">if</span> (program.del) <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;调用了 -d 参数，我们可以对 -d 参数做具体处理&#x27;</span>)

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;验证 program.delete 属性值：&#x27;</span>, program.del);</code></pre></div>
<p><a target="_blank" rel="noopener" href="https://my.oschina.net/dkvirus/blog/1204210">完整的博客</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/commander/">commander</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2019/12/21/typescript%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">typescript数据类型</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2019/12/17/%E4%BD%BF%E7%94%A8js%E5%AE%9E%E7%8E%B0MQTT/">
                        <span class="hidden-mobile">使用js实现MQTT</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->

  <script  src="https://fastly.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://fastly.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://fastly.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script  src="https://fastly.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://fastly.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://fastly.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://fastly.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>


  <script defer src="/js/leancloud.js" ></script>



  <script  src="https://fastly.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>












  

  

  

  

  

  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
